package com.tepth.appmanageservice.protocol.bodys.ordinaryfile;

import com.tepth.appmanageservice.protocol.interfaces.ISend;
import com.tepth.appmanageservice.tools.ext.ExtToBytes;

public class ViewOrdinaryFileListReply implements ISend {
	public static final short ID = 0x0C24;

	private short replySerialNumber;// Ӧ����ˮ��
	private byte fileType;// �ļ�����
	private OrdinaryFile[] fileList;
	private byte[] data;

	public short getReplySerialNumber() {
		return this.replySerialNumber;
	}

	public void setReplySerialNumber(short replySerialNumber) {
		this.replySerialNumber = replySerialNumber;
	}

	public byte getFileType() {
		return this.fileType;
	}

	public void setFileType(byte fileType) {
		this.fileType = fileType;
	}

	public OrdinaryFile[] getFileList() {
		return this.fileList;
	}

	public void setFileList(OrdinaryFile[] fileList) {
		this.fileList = fileList;
	}

	@Override
	public short getId() {
		return ID;
	}

	@Override
	public short getLength() {
		return (short) this.data.length;
	}

	@Override
	public byte[] getResult() {
		return this.data;
	}

	@Override
	public void load() {
		int fileCount = this.fileList.length;
		byte[][] datas = new byte[fileCount + 3][];
		datas[0] = ExtToBytes.fromNumber(this.replySerialNumber);
		datas[1] = new byte[] { this.fileType };
		datas[2] = new byte[] { (byte) fileCount };
		for (int i = 0; i < fileCount; i++) {
			datas[3 + i] = this.fileList[i].toBytes();
		}

		this.data = ExtToBytes.fromByteArray2(datas);
	}

	public static class OrdinaryFile {
		private String name;
		private int fileSize;
		private String md5;

		public OrdinaryFile(String name, int fileSize, String md5) {
			this.name = name;
			this.fileSize = fileSize;
			this.md5 = md5;
		}

		public byte[] toBytes() {
			byte[] nameBytes = ExtToBytes.fromStringForCoding(this.name);

			byte[][] byteArray = new byte[4][];
			byteArray[0] = new byte[] { (byte) nameBytes.length };
			byteArray[1] = nameBytes;
			byteArray[2] = ExtToBytes.fromNumber(this.fileSize);
			byteArray[3] = ExtToBytes.fromString16(this.md5);

			return ExtToBytes.fromByteArray2(byteArray);
		}
	}
}